package com.example.banksys;

import DataBase.myDB;
import handler.queryHandler;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.*;
import javafx.util.Callback;

import java.sql.ResultSet;
import java.sql.SQLException;

public class EmployeeTradeController {
    public TextField id;
    public ComboBox<String> Organization;

    public ObservableList<employeeTradeTrace> resList;
    public TableView<employeeTradeTrace> tradeTable;
    public TableColumn<employeeTradeTrace,Number> tradeIDCol;
    public TableColumn<employeeTradeTrace,String> tradeTypeCol;
    public TableColumn<employeeTradeTrace,Number> tradeMoneyCol;
    public TableColumn<employeeTradeTrace,String> tradeTimeCol;
    public TableColumn<employeeTradeTrace,Number> tellerIDCol;
    public TableColumn<employeeTradeTrace,String> tellerNameCol;
    public TableColumn<employeeTradeTrace,String> organizationCol;
    public Button query;

    public void initialize() throws SQLException {
        resList= FXCollections.observableArrayList();
        queryHandler qhandler=new queryHandler();
        qhandler.extensiveEmployeeTradeQuery(id,Organization,resList);
        tradeTable.setItems(resList);
        Organization.getItems().add("all");
        myDB database=new myDB("banksystem");
        String sql="select distinct organization from employee";
        ResultSet resultSet=database.getStatement().executeQuery(sql);
        while(resultSet.next()){
            Organization.getItems().add(resultSet.getString(1));
        }

        tradeIDCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<employeeTradeTrace, Number>, ObservableValue<Number>>() {
            @Override
            public ObservableValue<Number> call(TableColumn.CellDataFeatures<employeeTradeTrace, Number> employeeTradeTraceNumberCellDataFeatures) {
                return employeeTradeTraceNumberCellDataFeatures.getValue().getTradeID();
            }
        });
        tradeTypeCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<employeeTradeTrace, String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<employeeTradeTrace, String> employeeTradeTraceStringCellDataFeatures) {
                return employeeTradeTraceStringCellDataFeatures.getValue().getTradeType();
            }
        });
        tradeMoneyCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<employeeTradeTrace, Number>, ObservableValue<Number>>() {
            @Override
            public ObservableValue<Number> call(TableColumn.CellDataFeatures<employeeTradeTrace, Number> employeeTradeTraceNumberCellDataFeatures) {
                return employeeTradeTraceNumberCellDataFeatures.getValue().getTradeMoney();
            }
        });
        tradeTimeCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<employeeTradeTrace, String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<employeeTradeTrace, String> employeeTradeTraceStringCellDataFeatures) {
                return new SimpleStringProperty(employeeTradeTraceStringCellDataFeatures.getValue().getTradeDate().toString());
            }
        });
        tellerIDCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<employeeTradeTrace, Number>, ObservableValue<Number>>() {
            @Override
            public ObservableValue<Number> call(TableColumn.CellDataFeatures<employeeTradeTrace, Number> employeeTradeTraceNumberCellDataFeatures) {
                return employeeTradeTraceNumberCellDataFeatures.getValue().getTellerID();
            }
        });
        tellerNameCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<employeeTradeTrace, String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<employeeTradeTrace, String> employeeTradeTraceStringCellDataFeatures) {
                return employeeTradeTraceStringCellDataFeatures.getValue().getTellerName();
            }
        });
        organizationCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<employeeTradeTrace, String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<employeeTradeTrace, String> employeeTradeTraceStringCellDataFeatures) {
                return employeeTradeTraceStringCellDataFeatures.getValue().getTellerOrganization();
            }
        });

    }
    
    public void reQuery() throws SQLException {
        queryHandler req=new queryHandler();
        ObservableList<employeeTradeTrace> realList=FXCollections.observableArrayList();
        req.extensiveEmployeeTradeQuery(id,Organization, realList);
        tradeTable.getItems().remove(0,tradeTable.getItems().size());
        tradeTable.setItems(realList);
    }
}
